package cn.coisini.controller.v1;

import cn.coisini.api.admin.UserControllerApi;
import cn.coisini.model.common.annotation.RedisAccessLimit;
import cn.coisini.model.common.dtos.Result;
import cn.coisini.model.common.enums.ResultEnum;
import cn.coisini.model.common.log.annotation.Log;
import cn.coisini.model.common.log.enums.BusinessType;
import cn.coisini.model.dto.UserDto;
import cn.coisini.model.pojo.User;
import cn.coisini.model.vo.AssginRoleVo;
import cn.coisini.model.vo.QueryVo;
import cn.coisini.model.vo.UserVo;
import cn.coisini.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author xiangshaw
 * Description: 用户 控制层
 */
@RestController
@RequiredArgsConstructor
public class UserController implements UserControllerApi {

    private final UserService userService;

    // 条件分页查询用户信息
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Result<Object> pagingQuery(QueryVo queryVo) {
        return userService.pagingQuery(queryVo);
    }

    // 根据用户id查询用户信息
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Result<UserDto> findUserById(UserVo userVo) {
        return userService.findById(userVo.getId());
    }

    // 根据用户id列表查询用户信息
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public List<User> findUserByIds(UserVo userVo) {
        return userService.findByIds(userVo.getIds());
    }

    // 根据用户ID列表批量获取用户信息，并以用户ID为键构建Map
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Map<String, User> getUserMapByIds(UserVo userVo) {
        return userService.getUserMapByIds(userVo.getIds());
    }

    // 根据用户名查询用户信息
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Result<Map<String, Object>> findByUsername() {
        return userService.getUserInfoByUserName();
    }

    // 用户信息更新接口
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    public Result<ResultEnum> updateUserInfo(UserDto userDto) {
        return userService.updateUserInfo(userDto);
    }

    // 用户状态更新
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "用户管理", businessType = BusinessType.STATUS)
    public Result<ResultEnum> updateUserStatus(UserVo userVo) {
        return userService.updateUserStatus(userVo.getStatus());
    }

    // 用户头像更新
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    public Result<ResultEnum> updateUserAvatar(UserVo userVo) {
        return userService.updateUserAvatar(userVo.getAvatar());
    }

    // 用户密码更新
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    public Result<ResultEnum> updateUserPassword(UserVo userVo) {
        return userService.updateUserPassword(userVo);
    }

    // 绑定手机号
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    public Result<ResultEnum> bindPhone(UserVo userVo) {
        return userService.bindPhone(userVo);
    }

    // 核查用户手机号
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "用户管理", businessType = BusinessType.SELECT)
    public boolean checkUserPhone(String phone) {
        return userService.checkUserPhone(phone);
    }

    // 删除用户
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    public Result<ResultEnum> remove(String id) {
        return userService.removeUser(id);
    }

    // 批量删除用户
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    public Result<ResultEnum> batchRemove(List<String> ids) {
        return userService.batchRemove(ids);
    }

    // 添加用户
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    public Result<ResultEnum> addUser(UserDto userDto) {
        return userService.addUser(userDto);
    }

    // 修改用户
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    public Result<ResultEnum> updateUser(UserDto userDto) {
        return userService.updateUser(userDto);
    }

    // 根据用户id查询用户角色
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Result<Map<String, Object>> getRolesByUserId(String id) {
        return userService.getRolesByUserId(id);
    }

    // 分配角色
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "用户管理", businessType = BusinessType.CAST)
    public Result<ResultEnum> doAssign(AssginRoleVo assginRoleVo) {
        return userService.doAssign(assginRoleVo);
    }
}
